//
//  LMEGoodsModel.m
//  WelfareManager
//
//  Created by longmin on 17/2/27.
//  Copyright © 2017年 timtian. All rights reserved.
//

#import "LMEGoodsModel.h"
#import "LMESchemePageModel.h"

@implementation LMEGoodsModel

+ (void)removeGoodsWithMainList:(NSMutableArray *)goodsList removedGoods:(LMEGoodsModel *)goodsModel {
    
    for (LMEGoodsModel *goods in goodsList) {
        if ([goods.product_id isEqualToString:goodsModel.product_id]) {
            [goodsList removeObject:goodsModel];
        }
    }
    
}

+ (void)addGoodsWithMainList:(NSMutableArray *)goodsList newGoods:(LMEGoodsModel *)goodsModel {
    
    for (LMEGoodsModel *goods in goodsList) {
        if ([goods.product_id isEqualToString:goodsModel.product_id]) {
            return;
        }
    }
    [goodsList addObject:goodsModel];
}

+ (NSMutableArray *)refreshGoodsList:(NSArray *)newlist selectList:(NSArray *)selectList {
    
    NSMutableArray *resultGoods = [NSMutableArray arrayWithArray:newlist];
    
    for (LMEGoodsModel *goods in resultGoods) {
        
        for (LMEGoodsModel *goodsSelected in selectList) {
            if ([goodsSelected.product_id isEqualToString:goods.product_id]) {
                goods.state = LMEGoodsStateSelected;
            }
        }
        
    }
    
    return resultGoods;
}

+ (NSMutableArray *)compareGoodsList:(NSArray *)goodsList SelectList:(NSArray *)selectList {
    
    NSMutableArray *resultGoods = [NSMutableArray arrayWithArray:goodsList];
    for (LMEGoodsModel *goods in resultGoods) {
        
        for (LMEGoodsModel *goodsInScheme in selectList) {
            
            if ([goods.product_id isEqualToString:goodsInScheme.product_id]) {
                goods.in_scheme = YES;
            }
            
        }
        
    }
    
    return resultGoods;
}

+ (instancetype)goodsWithPage:(LMESchemePageModel *)pageModel {
    
    NSDictionary *dic = [pageModel.detailObject mj_keyValues];
    
    LMEGoodsModel *goods = [LMEGoodsModel mj_objectWithKeyValues:dic];
    goods.product_id = pageModel.product_id;
    goods.in_scheme = YES;
    
    return goods;
}

+ (NSMutableArray *)goodsListWithPageList:(NSArray <LMESchemePageModel *> *)pageList {
    
    NSMutableArray *goodsList = [NSMutableArray array];
    
    for (LMESchemePageModel *pageModel in pageList) {
        
        if (pageModel.type == LMEPageTypeGoods) {
            
            LMEGoodsModel *goods = [LMEGoodsModel goodsWithPage:pageModel];
            [goodsList addObject:goods];
        }
    }
    
    return goodsList;
}

+ (NSArray *)mj_ignoredPropertyNames {
    return @[@"state"];
}

- (NSString *)description {
    return [self mj_JSONString];
}

@end
